package org.onionframework.faces.web.action.support;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.onionframework.core.Onion;
import org.onionframework.servlet.ServletContexts;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 共用的ModalPanel
 * 
 * @author allan
 *
 */
@Component
@Scope("view")
public class FormPanel implements Serializable {

	private static final long serialVersionUID = 1L;
	private String view = "/faces/blank.xhtml";
	private String render = "formPanelInclude";
	
	public static void init(String uri) {
		if(StringUtils.isBlank(uri))
			return;
		
		FormPanel formPanel = Onion.getBean(FormPanel.class,"formPanel");
		formPanel.setView(uri);
		setCloseOnError(true);
	}
	
	public static FormPanel getCurrentInstance(){
		return Onion.getBean(FormPanel.class,"formPanel");
	}

	public String getView() {
		return view;
	}

	public void setView(String view) {
		this.view = view;
	}

	/**
	 * @param closeOnError the closeOnError to set
	 */
	public static void setCloseOnError(boolean closeOnError) {
		ServletContexts.getRequest().setAttribute("onion.faces.FormPanel.closeOnError", true);
	}

	public Boolean getCloseOnError(){
		return Boolean.TRUE.equals(ServletContexts.getRequest().getAttribute("onion.faces.FormPanel.closeOnError"));
	}

	public String getRender() {
		return this.render;
	}

	public void setRender(String render) {
		this.render = render;
	}
	
}
